package com.lyy.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author 俞伟
 * @version 1.0
 * @date 2020/6/2
 * @description com.tongsheng.common.utils
 */
@Slf4j
public class JSONUtils {
    /**
     * 获取json指定字段
     */
    public static String getJsonInfo(String jsonString, String parameter) {
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        String value = null;
        try {
            value = jsonObject.get(parameter).toString();
        } catch (Exception ignored) {
            log.info("==============================>" + jsonString + "无" + parameter + "字段！");
        }
        return value;
    }


    /**
     * jsonStr 转换成map
     *
     * @param jsonStr
     * @return
     * @throws Exception
     */
    public static Map<String, Object> jsonStrToMap(String jsonStr) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = null;
        try {
            map = mapper.readValue(jsonStr, new TypeReference<HashMap<String, Object>>() {
            });
            Set<String> set = map.keySet();
            for (String key : set) {
                String values = map.get(key).toString();
                values = values.trim();
                map.put(key, values);
            }
        } catch (IOException e) {
            log.info("==============================>json字符串转map失败！");
        }
        return map;
    }


    /**
     * Map转String
     *
     * @param map
     * @return
     */
    public static String getMapToString(Map<String, String> map) {
        Set<String> keySet = map.keySet();
        //将set集合转换为数组
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        //给数组排序(升序)
        Arrays.sort(keyArray);
        //因为String拼接效率会很低的，所以转用StringBuilder
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keyArray.length; i++) {
            // 参数值为空，则不参与签名 这个方法trim()是去空格
            if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
                sb.append(keyArray[i]).append(":").append(String.valueOf(map.get(keyArray[i])).trim());
            }
            if (i != keyArray.length - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    /**
     * Map的值转String
     *
     * @param map
     * @return
     */
    public static String getMapValueToString(Map<String, String> map) {
        StringBuilder sb = new StringBuilder();
        for (String key : map.keySet()) {
            String value = map.get(key);
            if (StringUtils.isNotBlank(value)) {
                sb.append(value);
            }
        }
        return sb.toString().replace(" ", "").toLowerCase();
    }

    /**
     * 将map按key值升序排序[a-z]
     *
     * @param bean
     * @param <T>
     * @return
     */
    public static <T> Map<String, String> sortMap(Map<String, String> map) {
        return new TreeMap<String, String>(map);
    }

    /**
     * 对象转json时属性变大写
     *
     * @param jsonObject
     * @return
     */
    public static JSONObject jsonObjectUpperKeyCase(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        for (String s : jsonObject.keySet()) {
            object.put(s.substring(0, 1).toUpperCase() + s.substring(1), jsonObject.get(s));
        }
        return object;
    }

}
